/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bankingsystem;

import java.util.Hashtable;
import java.util.Scanner;

/**
 * ATM class specified the ATM process where customer transactions
 * here it provides the facility to withdraw money from saving & current accounts
 * @author malith
 */
public class ATM {

    private Hashtable<String, Customer> customers;                      //define a hashtable for customers
    private Hashtable<String, CurrentAccount> currentAccounts;          //define a hashtable for current accounts
    private Hashtable<String, SavingsAccount> savingsAccounts;          //define a hashtable for saving accounts
    private Customer customer;                                          //define a customer
    Bank newbank;

    ATM(Bank newBank) {                                     //get instance of Bank Object
        //initialize the variables
        this.newbank = newBank;
        customers = newBank.getCustomers();
        currentAccounts = newBank.getCurrentAccounts();
        savingsAccounts = newBank.getSavingsAccounts();
        System.out.println("-------Welcome--------\n");
        login();
    }

    /**
     * login method specified for validate the customer login
     *
     */
    private void login() {

        try {
            System.out.println("Username:");                                  //get the username
            Scanner in = new Scanner(System.in);
            String customerId = in.nextLine();
            System.out.println("Password:");                                  //get the passward
            String password = in.nextLine();
            customer = (Customer) customers.get(customerId);
            if (customer != null) {                                           //check weather customer exist
                if (customer.checkPassword(password)) {                       //verify the passward & proceed to transaction
                    preview();

                } else {
                    System.out.println("Invalid Password.Please Try Again ");  //if passward is not a match
                }
            } else {
                System.out.println("Invalid customer ID");                     //if customer not exist
            }
        } catch (Exception e) {
            System.out.println("Invalid input try again.");
        }
    }

    /**
     * preview method specified for let user to select account type and proceed
     *
     */
    private void preview() {
        try {
            System.out.println("Select the account Type(Saving/Current)\n");
            System.out.println("1.Saving");
            System.out.println("2.Current");
            Scanner in = new Scanner(System.in);
            String choice = in.nextLine();
            if (Integer.parseInt(choice) == 1) {            //validate & update the saving account withdrawal
                savingAWithdraw();
            } else if (Integer.parseInt(choice) == 2) {     //validate & update the current account withdrawal
                currentAWithdraw();
            } else {
                System.out.println("Invalid type,try again.");
            }
        } catch (Exception e) {
            System.out.println("Invalid Input,try again.");
        }
    }

    /**
     * cerrentAWithdraw method specified for let user to withdraw money from
     * current account & update the status of account
     */
    private void currentAWithdraw() {

        try {
            if (customer.getCurrentAccountNo() != null) {
                CurrentAccount c = (CurrentAccount) currentAccounts.get(customer.getCurrentAccountNo());
                System.out.println("Your Balance is:" + c.balance);
                System.out.println("Enter the amount to Withdraw:");                    //get the amount
                Scanner in = new Scanner(System.in);
                String amount = in.nextLine();
                if (c.withdraw(Float.valueOf(amount))) {                                //Withdraw money from current account
                    System.out.println("Transaction Succesful\n");
                    System.out.println("Your new Balance is:" + c.balance);
                    newbank.addCurrentAccount(c);                                       //update the account status
                    new PersistenceManager().savedata(newbank);
                    System.out.println("Is there any other transactions(y/n)");         //check weather customer wants to proceed
                    in = new Scanner(System.in);
                    String other = in.nextLine();
                    if (other.equalsIgnoreCase("y")) {                                  //proceed to main view
                        preview();
                    } else {
                        System.out.println("Thank you");                                //Exit
                        System.exit(0);
                    }
                } else {
                    System.out.println("Transaction Failed/n");                         //if transaction failed proceed to main view
                    preview();
                }
            } else {
                System.out.println("You don't have a current account/n");               //if don't have a current account roceed to main view
                preview();
            }
        } catch (Exception e) {
            System.out.println("Invalid input,try again.");
        }
    }

    /**
     * savingAWithdraw method specified for let user to withdraw money from
     * current account & update the status of account
     */
    private void savingAWithdraw() {

        try {
            if (customer.getSavingsAccountNo() != null) {
                SavingsAccount s = (SavingsAccount) savingsAccounts.get(customer.getSavingsAccountNo());
                System.out.println("Your Balance is:" + s.balance);
                System.out.println("Enter the amount to Withdraw:");                //get the amount
                Scanner in = new Scanner(System.in);
                String amount = in.nextLine();

                if (s.withdraw(Float.valueOf(amount))) {                             //Withdraw money from saving account
                    System.out.println("Transaction Succesful\n");
                    System.out.println("Your new Balance is:" + s.balance);
                    newbank.addSavingsAccount(s);                                         //update the account status
                    new PersistenceManager().savedata(newbank);
                    System.out.println("Is there any other transactions(y/n)");      //check weather customer wants to proceed
                    in = new Scanner(System.in);
                    String other = in.nextLine();
                    if (other.equalsIgnoreCase("y")) {                                //proceed to main view
                        preview();
                    } else {
                        System.out.println("Thank you");                               //Exit
                        System.exit(0);
                    }
                } else {
                    System.out.println("Transaction Failed\n");                         //if transaction failed proceed to main view
                    preview();
                }
            } else {
                System.out.println("You don't have a saving account\n");                //if don't have a saving account roceed to main view
                preview();
            }
        } catch (Exception e) {
            System.out.println("Invalid input,try again.");
        }
    }
}
